<?php
$loader->requireOnce('includes/CommonConstant.class.php');
/**
 * VitalSign.
 * @package module.dept_emer.includes
 * @author hdviet
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class VitalSign extends ORDataObject {

	/**#@+
	 * Fields of table: vital_sign mapped to class members
	 */
	var $vital_sign_id		= '';
	var $examine_time		= '';
	var $pulse		= '';
	var $blood_presure_s		= '';
	var $blood_presure_d		= '';
	var $respiratory_rate		= '';
	var $temperature		= '';
	var $sp02		= '';
	var $ticket_id		= '';
	var $patient_id		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'vital_sign';

	/**
	 * Primary Key
	 */
	var $_key = 'vital_sign_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'VitalSign';

	/**
	 * Handle instantiation
	 */
	function __construct(){
		parent::ORDataObject();
	}
	/**
	 * Get the lasted vital sign of a given patient_id
	 * @param $patientId
	 * @return array(vital_sign_id, examine_time, pulse, blood_presure_s, blood_presure_d,
	 respiratory_rate, temperature, spo2, ticket_id)
	 */
	public function getLastestVitalSign($patientId) {
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "vital_sign_id, examine_time, pulse, blood_presure_s, blood_presure_d,
                        respiratory_rate, temperature, sp02, ticket_id",
            'from'  => "$this->_table", 
            'where' => "patient_id = '" . $patientId ."' AND examine_time IN (SELECT MAX(examine_time) FROM $this->_table t1 WHERE t1.patient_id='$patientId' )"
		);
		$ds->setup($this->_db, $sql);
		$result = $ds->toArray();
		if (empty($result)) {
			return null;
		}
		return $result[0];

	}

	/**
	 * Convert null number to displayable value (-1) -> ''
	 * @param $vitalSign
	 * @return nothing.
	 */
	public static function normalizeData4Display(&$vitalSign) {
		if (is_array($vitalSign)) {
			if ($vitalSign['pulse'] == CommonConstant::DB_NULL_POSITIVE_NUMBER) {
				$vitalSign['pulse'] = CommonConstant::DB_NULL_DISPLAY_POSITIVE_NUMBER;
			}

			if ($vitalSign['blood_presure_s'] == CommonConstant::DB_NULL_POSITIVE_NUMBER) {
				$vitalSign['blood_presure_s'] = CommonConstant::DB_NULL_DISPLAY_POSITIVE_NUMBER;
			}

			if ($vitalSign['blood_presure_d'] == CommonConstant::DB_NULL_POSITIVE_NUMBER) {
				$vitalSign['blood_presure_d'] = CommonConstant::DB_NULL_DISPLAY_POSITIVE_NUMBER;
			}

			if ($vitalSign['respiratory_rate'] == CommonConstant::DB_NULL_POSITIVE_NUMBER) {
				$vitalSign['respiratory_rate'] = CommonConstant::DB_NULL_DISPLAY_POSITIVE_NUMBER;
			}

			if ($vitalSign['temperature'] == CommonConstant::DB_NULL_POSITIVE_NUMBER) {
				$vitalSign['temperature'] = CommonConstant::DB_NULL_DISPLAY_POSITIVE_NUMBER;
			}
			if ($vitalSign['sp02'] == CommonConstant::DB_NULL_POSITIVE_NUMBER) {
				$vitalSign['sp02'] = CommonConstant::DB_NULL_DISPLAY_POSITIVE_NUMBER;
			}
			
			$vitalSign['examine_time'] = L10nHelper::renderDateTime($vitalSign['examine_time'],'short','datetime');
            $vitalSign['temperature'] = L10nHelper::renderNumber($vitalSign['temperature'],1);
            $vitalSign['sp02'] = L10nHelper::renderNumber($vitalSign['sp02'],1);
			

		}
	}
	
	
	/**
	 * Get the list of vital sign of a given patient_id
	 * @param $patientId
	 * @return array(vital_sign_id, examine_time, pulse, blood_presure_s, blood_presure_d,
	 respiratory_rate, temperature, sp02, ticket_id)
	 */
	public function listOfVitalSign($patientId) {
		$ds = new Datasource_sql();
		$sql = array(
            'cols'  => "vital_sign_id, examine_time, pulse, blood_presure_s, blood_presure_d,
                        respiratory_rate, temperature, sp02, ticket_id",
            'from'  => "$this->_table", 
            'where' => "patient_id = '" . $patientId ."'",
            'orderby' => 'examine_time DESC'
        
            );
            $ds->setup($this->_db, $sql);
            $result = $ds->toArray();
            return $result;
	}

	/**
	 * Add new empty vital sign.
	 * @param $patientId
	 * @return vital_sign_id.
	 */
	public function addNewVitalSign($patientId,$examineTime) {
		
		 $data = array('patient_id'=>$patientId,
		 'examine_time'=>$examineTime,
		 'pulse'=>CommonConstant::DB_NULL_POSITIVE_NUMBER,
		 'blood_presure_s'=>CommonConstant::DB_NULL_POSITIVE_NUMBER,
		 'blood_presure_d'=>CommonConstant::DB_NULL_POSITIVE_NUMBER,
		 'respiratory_rate'=>CommonConstant::DB_NULL_POSITIVE_NUMBER,
		 'temperature'=>CommonConstant::DB_NULL_POSITIVE_NUMBER,
		 'sp02'=>CommonConstant::DB_NULL_POSITIVE_NUMBER);

		$this->populateArray($data);
		$this->persist();
		return $this->get_id();
	}

	/**
	 * del vital sign.
	 * @param $patientId
	 * @return vital_sign_id.
	 */
	public function delVitalSign($vitalSignId) {
		$sql = "DELETE FROM $this->_table WHERE vital_sign_id = '" . $vitalSignId . "'";
		$this->_execute($sql);
	}
	/**
	 *
	 * Save vital sign
	 * @param $vitalSignId
	 * @param $data
	 * @return unknown_type
	 */
	public function saveVitalSign($vitalSignId,$data=array()) {

		$this->setup($vitalSignId);
		$this->populateArray($data);
		$this->persist();

	}
	
	

}
?>
 